package ui;

import geography.Maze;
import graphics.MazePanel;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;

public class GameGUI {
	
	private GameWindow gameWindow;
	private ButtonPanel menuButtonPanel;
	private Maze gameMaze = null;
	private MazePanel mazeContainer;
	private boolean gameRunning = false;
	private TitlePanel title;
	
	private int depthLevel = 1;
	
	/**
	 * Initializes the game-interface using a standard 720x480 size.
	 * @throws Exception if the size is smaller than 5x5.
	 */
	public GameGUI(){
		gameWindow = new GameWindow("Maze Game");
		
		menuButtonPanel = new ButtonPanel();
	}
	
	/**
	 * Shows the initial screen and start/exit-menu.
	 */
	public void showMenu(){
		
		gameWindow.getContentPane().removeAll();
		
		gameWindow.add(menuButtonPanel);
		
		gameWindow.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		menuButtonPanel.setLayout(new BoxLayout(menuButtonPanel, BoxLayout.PAGE_AXIS));

		//title image
		title = new TitlePanel();
		menuButtonPanel.add(title);
		
		//start-button
		StartButton start = new StartButton(this);
		menuButtonPanel.add(start);
		
		//padding
		menuButtonPanel.add(Box.createRigidArea(new Dimension(100, 20)));
		
		//exit-button
		ExitButton exit = new ExitButton();
		menuButtonPanel.add(exit);
		
		gameWindow.pack();
		//window is centered on the screen
		gameWindow.setLocationRelativeTo(null);
		//window is displayed
		gameWindow.setVisible(true);
		gameWindow.setResizable(false);
	}
	
	/**
	 * Erases the menu-components and initializes the maze.
	 */
	void showGame(){
		//if a game is currently running, no new maze can be created
		if(gameRunning){
			return;
		}
		gameRunning = true;
		
		//all elements are removed
		gameWindow.getContentPane().removeAll();
		
		//maze is initialized
		try {
			gameMaze = new Maze(26, 17);
		}
		catch (Exception e) {
			System.out.println("Error: "+e);
		}
		//the maze-panel and all its elements (player etc.) are initialized
		mazeContainer = new MazePanel(gameMaze, this, 1);
		
		//game-window is repacked and redrawn
		gameWindow.setLayout(new FlowLayout(FlowLayout.CENTER));
		gameWindow.add(mazeContainer);
		gameWindow.revalidate();
		gameWindow.repaint();
	}
	
	public void newLevel(){
		gameRunning = false;
		if(depthLevel == 99){
			showMenu();
			return;
		}
		
		depthLevel++;
		
		gameWindow.getContentPane().removeAll();
		
		try {
			gameMaze = new Maze(26, 17);
		}
		catch (Exception e) {
			System.out.println("Error: "+e);
		}
		//the maze-panel and all its elements (player etc.) are initialized
		mazeContainer = new MazePanel(gameMaze, this, depthLevel);
		
		//game-window is repacked and redrawn
		gameWindow.setLayout(new FlowLayout(FlowLayout.CENTER));
		gameWindow.add(mazeContainer);
		gameWindow.revalidate();
		gameWindow.repaint();
		
		gameRunning = true;
	}

}
